//
//  VideoCell.swift
//  UlTableView-Programmatic
//
//  Created by jack on 2025/10/28.
//

import UIKit

class VideoCell: UITableViewCell {
    var videoImageView = UIImageView()
    var videoTileLabel = UILabel()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        addSubview(videoImageView)
        addSubview(videoTileLabel)
        configureImageView()
        configureTitleLaber()
        setImageConstraints()
        setTitleLabelConstraints()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func set(video:Video) {
        videoImageView.image = video.image
        videoTileLabel.text = video.title
    }
    
    func configureImageView() {
        videoImageView.layer.cornerRadius = 10
        videoImageView.clipsToBounds = true
    }
    
    func configureTitleLaber() {
        videoTileLabel.numberOfLines = 0
        videoTileLabel.adjustsFontSizeToFitWidth = true
    }
    
    func setImageConstraints() {
        // 关闭自动生成约束，使用 Auto Layout
        videoImageView.translatesAutoresizingMaskIntoConstraints = false
        
        // 垂直居中对齐 cell
        videoImageView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        
        // 左边距 12pt
        videoImageView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 12).isActive = true
        
        // 高度固定为 80pt
        videoImageView.heightAnchor.constraint(equalToConstant: 80).isActive = true
        
        // 宽高比固定为 16:9
        videoImageView.widthAnchor.constraint(equalTo: videoImageView.heightAnchor, multiplier: 16/9).isActive = true
    }

    // 设置视频标题标签的约束
    func setTitleLabelConstraints() {
        // 关闭自动生成约束，使用 Auto Layout
        videoTileLabel.translatesAutoresizingMaskIntoConstraints = false
        
        // 垂直居中对齐 cell
        videoTileLabel.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        
        // 左边距与 imageView 右边相隔 20pt
        videoTileLabel.leadingAnchor.constraint(equalTo: videoImageView.trailingAnchor, constant: 20).isActive = true
        
        // 高度固定为 80pt，与 imageView 一致
        videoTileLabel.heightAnchor.constraint(equalToConstant: 80).isActive = true
        
        // 右边距固定为 12pt，保证不会超出 cell 宽度
        videoTileLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -12).isActive = true
    }

}
